package org.pcorp.battlespace.construction.ui.admin.arm;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;

import org.pcorp.battlespace.modele.Equipement;

public class ModaleLimitationArmAdmin extends JDialog {
	private static final long serialVersionUID = -1452690762361594957L;
	private Equipement equip;
	private JList list;

	public ModaleLimitationArmAdmin(Component parent, Equipement equip) {
		super(JOptionPane.getFrameForComponent(parent), true);
		this.setLayout(new GridBagLayout());
		this.setSize(150, 250);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setTitle("Limitation : " + equip.getNom());
		this.equip = equip;
		build();
	}

	public void build() {
		setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();

		gbc.gridx     = gbc.gridy      = 0;
		gbc.weightx   = gbc.weighty    = 1;
		gbc.gridwidth = gbc.gridheight = 1;
		gbc.fill = GridBagConstraints.BOTH;
		DefaultListModel model = new DefaultListModel();
		model.addElement("CHASSEUR");
		model.addElement("CABOTEUR");
		model.addElement("SAUTEUR");
		model.addElement("FRAPPEUR");
		model.addElement("STATION");
		list = new JList(model);
		list.setLayoutOrientation(JList.VERTICAL);
		list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		list.setVisibleRowCount(-1);

		add(list, gbc);
		
		 this.addWindowListener(new WindowAdapter(){

		        @Override
		        public void windowClosing(WindowEvent e) {
		        	Object[] liste = (Object[])list.getSelectedValues();
		        	equip.getRestrictions().clear();
		        	for (Object o : liste) {
						String s = (String) o;
						equip.getRestrictions().add(s);
					}
		        }
		    });
	}

	public static void openForm(Component parent, Equipement equip) {
		ModaleLimitationArmAdmin form = new ModaleLimitationArmAdmin(parent, equip);
		form.setVisible(true);
	}
}
